package com.qp.basics.common.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

public class DynamicDataSource extends AbstractRoutingDataSource
{
    private Map<Object, Object> targetDataSources;//实现动态添加
    private Map<String, String> dbnames = new HashMap<>();

    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources)
    {
        this.targetDataSources = targetDataSources;
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey()
    {
        return DynamicDataSourceContextHolder.getDataSourceType();
    }
    public void putDbname(String domain, String dbname){
        dbnames.put(domain,dbname);
    }
    public String getDbname(String domain){
        return  dbnames.get(domain);
    }
    public Map<Object, Object> getTargetDataSources() {
        return targetDataSources;
    }

}
